<template>
	<view class="lottery-container">
		<lottery-title :lotteryType="`fl`" />
		<view v-if="showTips" style="text-align: center;padding: 20rpx;"><text style="color: azure;">{{showTips}}</text>
		</view>
		<lottery-game ref="lotteryGame" :lotteryType="lotteryType" :pmType="pmType" :isDrawing="isDrawing"
			@play="onGamePlay" />
		<lottery-button ref="lotteryButton" :lotteryType="lotteryType" :title="zjTitle" :info="info"
			:prizeList="prizeList" :prePrizeList="prePrizeList" :showTips="showTips" />
		<lottery-process v-if="false" ref="lotteryProcess" :proceedList="proceedList" :pmzpzgInfo="pmzpzgInfo" @changeZP="onChangeZP"
			@startPmPlay="startPmPlay" @changePmType="onChangePmType" />
		<lottery-number v-if="false" :info="info"
			@ok="onCrowPrizeApply" />
			
		<view style="z-index: 401; padding: 10px 10px; border-radius: 20px; position: absolute; top: 20%; left: 5%; right: 5%; bottom: 2%; height: 70%; width: 85%; background-color: rgba(250,235,215,0.9);">
			<view style="display: flex; flex-direction: column; height: 100%;">
				<view style="flex:1; display: flex; flex-direction: row; padding-bottom: 10rpx;" @click="onSkip(`/pages/tab/rankingLottery?pmRankingType=dr`)">
					<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn-dr.png`" style="height: 150rpx;"  mode="heightFix"></image> 
					<view>
						<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn.png`" style="height: 150rpx;"  mode="heightFix"></image>
						<view style="font-size: 27rpx; text-align: center;color: black;">点击进入</view>
					</view>
				</view>
				<view style="flex:1; display: flex; flex-direction: row; padding-bottom: 10rpx;" @click="onSkip(`/pages/tab/rankingLottery?pmRankingType=sapp`)">
					<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn-sapp.png`" style="height: 150rpx;"  mode="heightFix"></image> 
					<view>
						<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn.png`" style="height: 150rpx;"  mode="heightFix"></image>
						<view style="font-size: 27rpx; text-align: center; color: black;">点击进入</view>
					</view>
				</view>
				<view style="flex:1; display: flex; flex-direction: row; padding-bottom: 10rpx;" @click="onSkip(`/pages/tab/rankingLottery?pmRankingType=city`)">
					<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn-city.png`" style="height: 150rpx;"  mode="heightFix"></image> 
					<view>
						<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn.png`" style="height: 150rpx;"  mode="heightFix"></image>
						<view style="font-size: 27rpx; text-align: center;color: black;">点击进入</view>
					</view>
				</view>
				<view style="flex:1; display: flex; flex-direction: row; padding-bottom: 10rpx;" @click="onSkip(`/pages/tab/lottery`)">
					<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn-dzp.png`" style="height: 150rpx;"  mode="heightFix"></image> 
					<view>
						<image :src="`https://wx.2024csnl.com/static/lottery/lottery-btn.png`" style="height: 150rpx;"  mode="heightFix"></image>
						<view style="font-size: 27rpx; text-align: center;color: black;">点击进入</view>
					</view>
				</view>
			</view>
		</view>	
			
		<uni-popup ref="pmGiftPopup" type="center" style="z-index: 401;">
			<view class="flex giftBox">
				<image class="openGiftIcon" src="https://wx.2024csnl.com/static/mine/newOpenGift.png"></image>
				<view class="giftText">{{giftText}}</view>
				<image class="closeIcon" @click.stop="close('pmGiftPopup')" src="https://wx.2024csnl.com/static/mine/newClose.png">
				</image>
			</view>
		</uni-popup>
		<pay-keyboard v-if="false" :show_key="show_pay_keyboard" @hideFun="hidePayKeyboard" :show_forget="true"
			@getPassword="getPayPassword"></pay-keyboard>
	</view>
</template>

<script>
	import {
		getPrizeMsg,
		getPrizesMsg,
		getcrowPrizeMsg,
		getPmdzpzg,
		getRotate,
		crowPrizeApply,
		getWinprizelistnew,
		startGetPrizeMsg,
		getXydcjpagejiangpin,
		getAppConfigSync,
		checkpaypwdRequest
	} from "../../api/index.js"
	export default {
		data() {
			return {
				show_pay_keyboard: false,
				payPassword: '',
				nums: 1,
				timer: [],
				timerDzpPlay: [],
				isStopPlay: true,
				zjTitle: '',
				lotteryNums: 0,
				lotteryType: 'dzp',
				pmType: 'day',
				choujiang_id: 0,
				isShowLiuTongZhiDuiHuanConfig: false,
				isShowLiuTongZhiDuiHuan: true,
				prizeList: [],
				prePrizeList: [],
				info: {
					guize: ''
				},
				giftText: '',
				stage: '',
				list: [],
				proceedList: [],
				pmzpzgInfo: {
					"pm_day": {
						"mid": 0,
						"type": "pm_day",
						"status": 0,
						"choujiang_id": 0
					},
					"pm_week": {
						"mid": 0,
						"type": "pm_week",
						"status": 0,
						"choujiang_id": 0
					},
					"pm_month": {
						"mid": 0,
						"type": "pm_month",
						"status": 0,
						"choujiang_id": 0
					},
					"pm_quarter": {
						"mid": 0,
						"type": "pm_quarter",
						"status": 0,
						"choujiang_id": 0
					},
					"pm_year": {
						"mid": 0,
						"type": "pm_year",
						"status": 0,
						"choujiang_id": 0
					}
				},
				isDrawing: false,
				isInit: false,
				duration: '300000',
				pagenum: 1,
				noMoreData: false,
				innerAudioContext: null,
				canCkick: true,
				recordBack: false,
				phoneType: false,
				currentJiangping: 0,
				channel: ""
			}
		},
		computed: {
			showTips() {
				var tips = "";
				if (this.channel) {
					switch (this.channel) {
						case "IOS":
							tips = "Apple Inc."
							break;
						case "HW":
							tips = "华为"
							break;
						case "OPPO":
							tips = "OPPO"
							break;
						case "VIVO":
							tips = "VIVO"
							break;
						case "MI":
							tips = "小米"
							break;
					}

					return `本次抽奖活动与设备生产商${tips}公司无关`;
				}
				return "";
			}
		},
		onLoad() {
			const appConfig = getAppConfigSync()
			setTimeout(() => {
				this.isShowLiuTongZhiDuiHuanConfig = appConfig.isShowLiuTongZhiDuiHuan
			}, 100)

			var channelVersionNumber = getApp().globalData.channelVersionNumber;
			var versionNumber = getApp().globalData.versionNumber;
			if (channelVersionNumber == versionNumber)
				this.channel = ""
			else
				this.channel = channelVersionNumber.replace("Public.", "").replace("." + versionNumber, "")
		},
		methods: {
			async showPayKeyboard() {
				this.show_pay_keyboard = true
			},
			hidePayKeyboard() {
				this.show_pay_keyboard = false
			},
			async getPayPassword(n) {
				console.log("用户输入的密码", n)
				this.payPassword = n.password;
				this.show_pay_keyboard = false

				const res = await checkpaypwdRequest('s=/ApiMy/checkpaypwd', {
					paypwd: this.payPassword
				})
				const {
					id,
					status,
					msg
				} = res.data
				console.log(res)
				if (status == 1) {
					await this.crowPrizeApplyPost()
				} else {
					this.showToast(msg)
				}
			},
			async startDrawing() {
				this.$refs.lotteryGame.init()
				this.isDrawing = true;
				this.playMusic();
			},
			async stopDrawing(awardIndex) {
				if (awardIndex) {
					this.$refs.lotteryGame.showPlay(awardIndex)
				}

				setTimeout(() => {
					this.isDrawing = false;
					this.pauseMusic()
				}, 5000);
			},
			async onChangeZP(zp) {
				this.lotteryType = zp;
				if (zp == 'pm') {
					await this.pmInit()
				} else {
					await this.dzpInit()
				}
			},
			async onGamePlay() {
				if (this.lotteryType == "pm") {
					this.onStartGetPmPrizeMsg()
				}
			},
			clearTimer() {
				for (var i = 0; i < this.timer.length; i++) {
					clearInterval(this.timer[i]);
				}
				for (var i = 0; i < this.timerDzpPlay.length; i++) {
					clearInterval(this.timerDzpPlay[i]);
				}

				this.timer = [];
				this.timerDzpPlay = [];
			},
			async dzpInit() {
				this.isInit = true
				this.isStopPlay = true
				this.currentJiangping = 0
				this.stopDrawing()
				this.clearTimer()
				this.isShowLiuTongZhiDuiHuan = true
				//this.onGetDzpPrizeMsg()
				this.$refs.lotteryProcess.changeTabsIndex(0)
			},
			async pmInit() {
				this.clearTimer()
				this.isShowLiuTongZhiDuiHuan = false
				this.isStopPlay = true
				this.zjTitle = "中奖名单"
				this.onGetPmdzpzg()
				this.$refs.lotteryProcess.changeTabsIndex(1)
			},
			async startPmPlay(choujiang_id) {
				this.choujiang_id = choujiang_id;
				await this.onGetPmPrizeMsg();
			},
			async onChangePmType(pmType) {
				this.pmType = pmType;
			},
			async startDzpPlay(choujiang_id) {
				this.choujiang_id = choujiang_id;
				this.timer.push(setInterval(() => {
					this.getDzpPlay();
				}, 2000))
			},
			async getDzpPlay() {
				const res = await getXydcjpagejiangpin('s=/ApiChoujiang/xydcjpagejiangpin', {
					choujiang_id: this.choujiang_id
				})
				const {
					msg,
					data,
					status
				} = res.data
				//console.log(res)
				if (status == 0) {
					var info = JSON.parse(JSON.stringify(this.info));
					info.usescore = data.info.usescore;
					info.score = data.info.score;
					info.nowscore = data.info.nowscore;

					var proceedList = JSON.parse(JSON.stringify(this.proceedList));

					proceedList[0].usescore = data.info.usescore;
					proceedList[0].score = data.info.score;
					proceedList[0].nowscore = data.info.nowscore;

					this.proceedList = proceedList;
				} else if (status == 1) {
					this.clearTimer()
					this.list = data.jiangping.map(item => {
						return {
							jx: item.jx,
							name: item.mc,
							img: item.pic,
							tp: item.tp,
							jz: item.jz,
							zjlist: item.zjlist
						}
					})

					this.isStopPlay = false;
					setTimeout(() => {
						this.nextPlay()
					}, 100)
				}
			},
			async nextPlay() {
				if (this.currentJiangping >= this.list.length) {
					console.log("结束")
					this.isStopPlay = true
					await this.dzpInit()
					return;
				}

				this.startDrawing()

				setTimeout(() => {
					if (!this.isStopPlay)
						this.dzpPlayReward()
				}, 40000)
			},
			async dzpPlayReward() {
				this.stopDrawing(this.currentJiangping)
				if (this.isStopPlay)
					return
				if (this.currentJiangping < this.list.length) {
					if (!this.list[this.currentJiangping])
						return;
					var zjlist = this.list[this.currentJiangping].zjlist;

					if (zjlist && zjlist.length > 0) {
						this.prizeList = this.list[this.currentJiangping].zjlist

						var jxIndex = parseInt(this.list[this.currentJiangping].jx);

						var t = ["一", "二", "三", "四", "五", "六", "七", "八"]

						this.zjTitle = t[jxIndex - 1] + "等奖"

						setTimeout(() => {
							this.showWiner();
						}, 5000)
					}
				}
			},
			async showWiner() {
				if (this.isStopPlay)
					return
				this.$refs.lotteryButton.showWinerDialog()
				setTimeout(() => {
					this.$refs.lotteryButton.dialogWinerCallback()
					this.currentJiangping = this.currentJiangping + 1;
					this.nextPlay()
				}, 3000)
			},
			async onGetDzpPrizeMsg() {
				const res = await getcrowPrizeMsg(`s=/ApiChoujiang/xydcjpage&pagenum=${this.pagenum}`)
				const {
					msg,
					data,
					status,
				} = res.data
				if (status == 1) {
					this.list = data.jiangping.map(item => {
						return {
							name: item.mc,
							img: item.pic,
							tp: item.tp,
							jz: item.jz
						}
					})
					this.proceedList = data.data
					this.pagenum != 1 && data.length == 0 ? this.noMoreData = true : this.noMoreData = false

					this.info = data.info;
					this.startDzpPlay(this.info.id)
					if (this.proceedList.length > 0) {
						this.stage = this.proceedList[0].stage
						this.onGetWinprizelistnew()
					}
				} else {
					this.showToast(msg)
				}
			},
			async onGetWinprizelistnew() {
				if (this.stage) {
					const res = await getWinprizelistnew(`s=/ApiChoujiang/winprizelistnew&stage=${this.stage}`)
					const {
						msg,
						status,
						data,
					} = res.data
					if (status == 1) {
						this.prePrizeList = data
					} else {
						this.showToast(msg)
					}
				}
			},
			//排名转盘逻辑
			async onGetPmdzpzg(type) {
				const res = await getPmdzpzg(`s=/ApiChoujiang/getpmdzpzg`)
				const {
					msg,
					data,
					status,
				} = res.data
				if (status == 1) {
					this.pmzpzgInfo = data
				}
				await this.onGetWinprizelistpm()
			},
			async onGetWinprizelistpm() {
				const winprizelistpmRes = await getPmdzpzg(`s=/ApiChoujiang/winprizelistpm`)
				const {
					msg,
					data,
					status,
				} = winprizelistpmRes.data
				if (status == 1) {
					this.prizeList = data
				}
			},
			async onGetPmPrizeMsg() {
				const res = await getPrizeMsg('s=/ApiChoujiang/index', {
					id: this.choujiang_id
				})
				const {
					msg,
					data,
					status
				} = res.data
				console.log("onGetPmPrizeMsg")
				console.log(res)
				if (status == 1) {
					this.list = data.jxarr.map(item => {
						return {
							name: item.mc,
							img: item.pic,
							tp: item.tp,
						}
					})
					this.prizeList = data.zjlist
					this.info = data.info
					this.lotteryNums = data.remaindaytimes
					if (this.lotteryNums > 0) {
						await this.startDrawing()
					}
				} else {
					await this.stopDrawing()
					this.showToast(msg)
				}
			},
			// 获取抽奖结果
			async onStartGetPmPrizeMsg() {
				if (this.lotteryNums == 0) {
					this.showToast('暂无抽奖次数')
					return
				}

				this.startDrawing()
				const res = await startGetPrizeMsg('s=/ApiChoujiang/index', {
					id: this.choujiang_id,
					op: 'getjx'
				})
				const {
					status,
					msg,
					data
				} = res.data
				console.log("startGetPrizeMsg")
				console.log(res)
				if (status == 1) {
					setTimeout(() => {
						this.pmPlayReward(data.jxindex, data.jx, data.text)
					}, 3000)
				} else {
					await this.stopDrawing()
					this.showToast(msg)
				}
			},
			//发起抽奖
			pmPlayReward(idx, isRate, text) {
				let that = this;
				var awardIndex = idx

				that.stopDrawing(awardIndex)

				if (that.lotteryNums != -1) {
					that.lotteryNums = that.lotteryNums - 1;
				}

				setTimeout(() => {
					that.open('pmGiftPopup')
					that.giftText = text
				}, 5000);
			},
			playMusic() {
				this.innerAudioContext = uni.createInnerAudioContext();
				this.innerAudioContext.autoplay = true; //自动播放
				this.innerAudioContext.loop = true; //循环播放
				this.innerAudioContext.src = 'https://wx.2024csnl.com/static/music.mp3';
				this.innerAudioContext.onPlay(() => { //可以播放事件
					this.playing = !this.innerAudioContext.paused; //查看是否可以自动播放
				});
				this.innerAudioContext.onError((res) => {});
			},
			pauseMusic() {
				if (this.innerAudioContext) {
					this.innerAudioContext.pause();
				}
			},
			async onCrowPrizeApply(nums) {
				this.applyNums = nums;
				await this.showPayKeyboard()
			},
			async crowPrizeApplyPost(){
				const res = await crowPrizeApply('s=/ApiChoujiang/signup', {
					num: this.applyNums
				})
				const {
					msg,
					data,
					status,
				} = res.data
				
				if (status == 1) {
					uni.showModal({
						title: '提示',
						content: '报名成功',
						showCancel: false,
					});
				} else {
					this.showToast(msg)
				}
			},
			open(n) {
				this.$nextTick(() => {
					setTimeout(() => {
						this.$refs[n].open();
					}, 1000)
				})
			},
			close(n) {
				if (n == 'pmGiftPopup') {
					this.$refs.lotteryGame.init()
				}
				this.$refs[n].close();
			},
		},

		onTabItemTap(e) {},
		onShow() {
			if (!this.isInit) {
				//this.clearTimer()
				this.lotteryType = "dzp"
				//this.dzpInit()
			}
		},
		onHide() {
			this.clearTimer()
			this.isInit = false
			this.isStopPlay = true
			this.clearTimer()
			this.pauseMusic()
		},
		onUnload() {
			this.isInit = false
			this.isStopPlay = true
			this.clearTimer()
			if (this.innerAudioContext) {
				this.innerAudioContext.stop()
			}
		}
	}
</script>

<style lang="less" scoped>
	.lottery-container {
		min-height: 100vh;
		background: url('https://wx.2024csnl.com/static/lottery/lottery-1.jpg') center top no-repeat #c20800;
		background-size: 100% auto;
	}


	.giftText {
		position: absolute;
		top: 33vh;
		width: 19.5vh;
		left: 33vw;
		color: #F40E2A;
		font-weight: 600;
	}

	.giftBox {
		width: 100vw;
		height: 50vh;
		flex-direction: column;
		position: relative;
	}

	.closeIcon {
		width: 70rpx;
		height: 70rpx;
		position: absolute;
		bottom: 0;
		left: 45vw;
	}

	.openGiftIcon {
		position: absolute;
		width: 130vw;
		height: 58vh;
		left: -15vw;
		bottom: -100rpx;
	}
</style>